package com.shop2cn.iapi.sqdistributor.testcase;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.ReplaceDistributorConsumerRelationReq;
import com.shop2cn.iapi.sqdistributor.service.ReplaceDistributorConsumerRelationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributorConsumerRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorConsumerRelationWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_ReplaceDistributorConsumerRelation {

	// java接口
	private static ReplaceDistributorConsumerRelationReq replaceDistributorConsumerRelationReq;
	private static ReplaceDistributorConsumerRelationCall replaceDistributorConsumerRelationCall;

	private static DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();
	private static DistributorConsumerRelationWrapper distributorConsumerRelationWrapper = new DistributorConsumerRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("重新绑定分销员与消费者间的关系");
	}

	@Before
	public void caseUp() {
		replaceDistributorConsumerRelationReq = new ReplaceDistributorConsumerRelationReq();
		replaceDistributorConsumerRelationCall = new ReplaceDistributorConsumerRelationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(replaceDistributorConsumerRelationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb(int oldCount, int newCount) throws Exception {
	
		DistributorConsumerRelation distributorConsumerRelation = distributorConsumerRelationWrapper
				.selectByPrimaryKey(replaceDistributorConsumerRelationReq
						.getId());

		Logger.verifyEquals(distributorConsumerRelation.getDistributor_id(),
				replaceDistributorConsumerRelationReq.getNewDistributorId(),
				"验证换绑NewDistributorId");

		int oldDistributorInviteCount = distributorRelationWrapper
				.selectByDistributorId(26278552L).getInvite_count();
		int newDistributorInviteCount = distributorRelationWrapper
				.selectByDistributorId(
						replaceDistributorConsumerRelationReq
								.getNewDistributorId()).getInvite_count();

		Logger.verifyEquals(oldCount - 1, oldDistributorInviteCount,
				"验证oldDistributorInviteCount");
		Logger.verifyEquals(newCount + 1, newDistributorInviteCount,
				"验证newDistributorInviteCount");
	}
	
	// 重置换绑记录
	public void resetBindRelation() throws ClientProtocolException, IOException {
		
		replaceDistributorConsumerRelationReq.setId(3188L);
		replaceDistributorConsumerRelationReq.setMerchantId(500002490L);
		replaceDistributorConsumerRelationReq.setNewDistributorId(26278552L);
		
		replaceDistributorConsumerRelationCall
				.setData(replaceDistributorConsumerRelationReq);
		replaceDistributorConsumerRelationCall.callService();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReplaceDistributorConsumerRelation_001() {
		Logger.start(true, "商户不存在，换绑失败");

		try {

			replaceDistributorConsumerRelationReq.setId(1L);
			replaceDistributorConsumerRelationReq.setMerchantId(24124215124L);
			replaceDistributorConsumerRelationReq
					.setNewDistributorId(501565931L);

			replaceDistributorConsumerRelationCall
					.setData(replaceDistributorConsumerRelationReq);
			replaceDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					replaceDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(String.format("分销员 [%s] 未找到",
					replaceDistributorConsumerRelationReq.getNewDistributorId()),
					replaceDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReplaceDistributorConsumerRelation_002() {
		Logger.start(true, "分销员不存在，绑定失败");

		try {

			replaceDistributorConsumerRelationReq.setId(1L);
			replaceDistributorConsumerRelationReq.setMerchantId(24124215124L);
			replaceDistributorConsumerRelationReq.setNewDistributorId(501565931L);

			replaceDistributorConsumerRelationCall
					.setData(replaceDistributorConsumerRelationReq);
			replaceDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					replaceDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(String.format("分销员 [%s] 未找到",
					replaceDistributorConsumerRelationReq.getNewDistributorId()),
					replaceDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReplaceDistributorConsumerRelation_003() {
		Logger.start(true, "消费者已经绑定临时关系，再次绑定其他分销员，换绑成功");

		try {

			int oldCount = distributorRelationWrapper.selectByDistributorId(
					26278552L).getInvite_count();
			
			int newCount = distributorRelationWrapper.selectByDistributorId(
					26277844L).getInvite_count();
			
			replaceDistributorConsumerRelationReq.setId(3188L);
			replaceDistributorConsumerRelationReq.setMerchantId(500002490L);
			replaceDistributorConsumerRelationReq.setNewDistributorId(26277844L);

			replaceDistributorConsumerRelationCall
					.setData(replaceDistributorConsumerRelationReq);
			replaceDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					replaceDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			
			verifyDb(oldCount, newCount);
			
			this.resetBindRelation();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReplaceDistributorConsumerRelation_004() {
		Logger.start(true, "换绑同一个人，换绑失败");

		try {
			
			replaceDistributorConsumerRelationReq.setId(3188L);
			replaceDistributorConsumerRelationReq.setMerchantId(500002490L);
			replaceDistributorConsumerRelationReq.setNewDistributorId(26278552L);

			replaceDistributorConsumerRelationCall
					.setData(replaceDistributorConsumerRelationReq);
			replaceDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					replaceDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("当前绑定关系已维护，请勿重新操作",
					replaceDistributorConsumerRelationCall.getMsg(),
					"验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(YMTDateUtil.diffDay(YMTDateUtil.getDate("2020-10-23 15:37:50", YMTDateUtil.YMD),
				YMTDateUtil.getDate(YMTDateUtil.getBeforeOrNextDay(0), YMTDateUtil.YMD)));
	}
}